<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;
use	App\Models\AppImg;
class AppController extends Controller
{
	   // 检测用户是否登录，无登录跳转到登录页面
    public function __construct()
    {
       $this->middleware('auth');
    }
    //APP页面管理
    public function index(){

    	$img = AppImg::get();
  
		return view('app.index',compact('img'));
    	
    }

    //App引导页更新
    public function update(Request $request,ImageUploadHandler $upload){

    	//上传文件夹路径
    	$folder = 'app_img';

    	// dd($request);exit;
    	//判断是否存在数据
    	if(!empty($request->filename1)){

    	 $file = $upload->save($request->filename1,$folder);

    	 $img = AppImg::where('id',1)->first();

	    	 if($img != null){

	    		 $status = AppImg::where('id',1)->update(['path'=>$file['path']]);

	    	 }else{

	    	 	 $status = AppImg::where('id',1)->insert(['path'=>$file['path']]);

	    	 }

	    	 if($status){

	    	 	return back();
	    	 }

    	}elseif(!empty($request->filename2)){

    		$file = $upload->save($request->filename2,$folder);

	    	 $img = AppImg::where('id',2)->first();

	    	 if($img != null){

	    		 $status = AppImg::where('id',2)->update(['path'=>$file['path']]);

	    	 }else{

	    	 	 $status = AppImg::where('id',2)->insert(['path'=>$file['path']]);

	    	 }
	    	 
	    	 if($status){

	    	 	return back();
	    	 }

    	}elseif(!empty($request->filename3)){

    		$file = $upload->save($request->filename3,$folder);

	    	 $img = AppImg::where('id',3)->first();

		    	 if($img != null){

		    		 $status = AppImg::where('id',3)->update(['path'=>$file['path']]);

		    	 }else{

		    	 	 $status = AppImg::where('id',3)->insert(['path'=>$file['path']]);

		    	 }
		    	 
		    	 if($status){

		    	 	return back();
		    	 }

    	}else{

    		return '文件为空';
    	}
    }
}
